package com.storm.lib.template;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.storm.lib.po.BaseRolePo;
import com.storm.lib.type.MapType;
import com.storm.lib.util.BeanUtil;
import com.storm.lib.vo.BaseMapAvatarVo;
@Controller
public class MapAvatarTemplate {
	
	@Autowired
	private RoleTemplate roleTemplate;
	/**
	 * 区域下得人物形象,key1 =区域id, value1=玩家map key2=玩家id key2=玩家avatar
	 */
	public static ConcurrentHashMap<Integer,ConcurrentHashMap<Integer, BaseMapAvatarVo>> areaAvatarsCache = new ConcurrentHashMap<Integer, ConcurrentHashMap<Integer,BaseMapAvatarVo>>();
	/**
	 * 所有玩家下的待更新列表 key1= 玩家id value1=某玩家的待更新玩家列表 key2=待更新玩家id value2=待更新项里的玩家 key3=更新项的行为编号 value3=更新项
	 */
	public static ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,Object[]>>> toUpdateAvatarCache =new ConcurrentHashMap<Integer, ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,Object[]>>>();
	/**
	 * 这个角色附近的有效Avatars
	 */
	public static ConcurrentHashMap<Integer, List<BaseMapAvatarVo>> roleIdFiltedRoundAvatarMapping = new ConcurrentHashMap<Integer, List<BaseMapAvatarVo>>();
	/**
	 * 
	 * 方法功能:周围的列表
	 * 更新时间:2011-11-4, 作者:johnny
	 * @return
	 */
	public ConcurrentHashMap<Integer, List<BaseMapAvatarVo>> getRoleIdFiltedRoundAvatarMapping(){
		return roleIdFiltedRoundAvatarMapping;
	}
	
	/**
	 * 
	 * 方法功能:更新地图的Avatar
	 * 
	 * 更新时间:2011-11-4, 作者:johnny
	 * @param mapAvatar
	 * @param areaId
	 */
	public void addAvatorToAreaMaps(BaseMapAvatarVo mapAvatar,Integer areaId) {
		ConcurrentHashMap<Integer, BaseMapAvatarVo> theAreaAvators = areaAvatarsCache.get(areaId);
		theAreaAvators.put(mapAvatar.getId(), mapAvatar);
	}
	
	/**
	 * 
	 * 方法功能:移除地图的Avatar
	 * 更新时间:2011-11-4, 作者:johnny
	 * @param areaId
	 * @param roleId
	 */
	public void removeAvatorToAreaMaps(Integer areaId, Integer roleId) {
		ConcurrentHashMap<Integer, BaseMapAvatarVo> theAreaAvators = areaAvatarsCache.get(areaId);
		theAreaAvators.remove(roleId);
	}	
	
	/**
	 * 
	 * 方法功能:发送进入场景的Change
	 * 更新时间:2011-8-22, 作者:johnny
	 * @param changerAvator
	 */
	public void sendAvatarUpdateToAreaOtherUser(BaseMapAvatarVo changerAvator,Integer mapId,Object[] change) {
		ConcurrentHashMap<Integer, BaseMapAvatarVo> theAreaAvators = areaAvatarsCache.get(mapId);
		//遍历区域的Avatar
		for (BaseMapAvatarVo aroundAvator : theAreaAvators.values()) {
			//如果变化的是自己,跳过
			if(aroundAvator.getId().intValue()==changerAvator.getId().intValue()){
				continue;
			}
			if(avatarInTheRolesFilterList(changerAvator,aroundAvator.getId())){
				continue;
			}
			ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,Object[]>> changeItem = toUpdateAvatarCache.get(aroundAvator.getId());
			//如果没有该目标用户,则增加
			if(changeItem==null){
				changeItem =new ConcurrentHashMap<Integer, ConcurrentHashMap<Integer,Object[]>>();
				toUpdateAvatarCache.put(aroundAvator.getId(), changeItem);
			}
			//如果没有该目标用户下的更新用户,则增加
			ConcurrentHashMap<Integer,Object[]> thePlayerAvator = changeItem.get(changerAvator.getId());
			if(thePlayerAvator==null){
				thePlayerAvator=new ConcurrentHashMap<Integer,Object[]>();
				changeItem.put(changerAvator.getId(), thePlayerAvator);
			}
			//[0]项就是类型
			if(thePlayerAvator!=null){
				thePlayerAvator.put((Integer)change[0], change);
			}
		}
	}
	
	
	
	/**
	 * 
	 * 方法功能:判断一个avatar是否在过滤器内
	 * 更新时间:2011-11-4, 作者:johnny
	 * @param changerAvator
	 * @param rolePo
	 * @return
	 */
	public boolean avatarInTheRolesFilterList(BaseMapAvatarVo changerAvator,Integer roleId) {
		List<BaseMapAvatarVo> theList =getRoleIdFiltedRoundAvatarMapping(roleId);
		if(theList==null){
			return false;
		}
		for (int i = 0; i < theList.size(); i++) {
			BaseMapAvatarVo mapAvatarVo = theList.get(i);
			if(mapAvatarVo.getId().intValue()==changerAvator.getId().intValue()){
				return true;
			}
		}
		return false;
	}


	/**
	 * 
	 * 方法功能:消息推送到客户端
	 * 更新时间:2011-8-23, 作者:johnny
	 * @param playerToUpdateListId
	 * @param toUpdate
	 */
	public void flushOutChanges(Integer playerToUpdateListId,List<Object[]> toUpdate) {
		if(toUpdate==null){
			return;
		}
		PushTemplate pushTemplate = (PushTemplate) BeanUtil.getBean("pushTemplate");
		String iuid = roleTemplate.roleIdIuidMapping.get(playerToUpdateListId);
		pushTemplate.pushArrayToRole(iuid, toUpdate.toArray(new Object[toUpdate.size()]));
		toUpdateAvatarCache.get(playerToUpdateListId).clear();
	}

	public ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,Object[]>>> getAroundAvatarCache() {
		return toUpdateAvatarCache;
	}

	public ConcurrentHashMap<Integer, BaseMapAvatarVo> getRoleIdAvatorMapping() {
		return roleTemplate.roleIdAvatorMapping;
	}
	
	public void removeRoleIdAvatarMapping(Integer roleId) {
		roleTemplate.roleIdAvatorMapping.remove(roleId);
	}

	public void removeIuidSessionMapping(String iuid) {
		roleTemplate.iuidSessionMapping.remove(iuid);
	}
	
	public ConcurrentHashMap<Integer,ConcurrentHashMap<Integer, BaseMapAvatarVo>> getAreaAvatarsCache() {
		return areaAvatarsCache;
	}
	
	
	public List<BaseMapAvatarVo> getRoleIdFiltedRoundAvatarMapping(Integer roleId) {
		return getRoleIdFiltedRoundAvatarMapping().get(roleId);
	}

	/**
	 * 
	 * 方法功能:为某一玩家-初始化他的过滤列表
	 * 更新时间:2011-11-4, 作者:johnny
	 * @param rolePo
	 */
	public void initSelfFilterMapping(BaseRolePo rolePo) {
		List<BaseMapAvatarVo> ints = new ArrayList<BaseMapAvatarVo>();
		ConcurrentHashMap<Integer, BaseMapAvatarVo> avatars =getAreaAvatarsCache().get(rolePo.getAreaId());
		for (BaseMapAvatarVo mapAvatarVo : avatars.values()) {
			if(ints.size()>=MapType.SCREEN_MAX){
				break;
			}
			if(mapAvatarVo.getId().intValue()==rolePo.getId().intValue()){
				continue;
			}
			ints.add(mapAvatarVo);
		}
		getRoleIdFiltedRoundAvatarMapping().put(rolePo.getId(), ints);
	}

	/**
	 * 
	 * 方法功能:更新区域玩家的过滤列表-增加某一用户
	 * 更新时间:2011-11-4, 作者:johnny
	 * @param rolePo
	 */
	public void sendAddToAreaOtherPlayersFilterMapping(BaseRolePo rolePo) {
		ConcurrentHashMap<Integer, BaseMapAvatarVo> list =getAreaAvatarsCache().get(rolePo.getAreaId());
		//为每个用户增添
		for (BaseMapAvatarVo mapAvatarVo : list.values()) {
			if(mapAvatarVo.getId().intValue()==rolePo.getId().intValue()){
				continue;
			}
			List<BaseMapAvatarVo> lists =getRoleIdFiltedRoundAvatarMapping().get(mapAvatarVo.getId());
			if(lists.size()>=MapType.SCREEN_MAX){
				continue;
			}
			lists.add(getRoleIdAvatorMapping().get(rolePo.getId()));
		}

	}

	/**
	 * 
	 * 方法功能:为某一玩家-清除他的过滤列表
	 * 更新时间:2011-11-4, 作者:johnny
	 * @param rolePo
	 */
	public void removeRoleIdFiltedRoundAvatarMapping(BaseRolePo rolePo) {;
		getRoleIdFiltedRoundAvatarMapping().remove(rolePo.getId());
		
	}


	/**
	 * 
	 * 方法功能:更新区域玩家的过滤列表-移除某一用户
	 * 更新时间:2011-11-4, 作者:johnny
	 * @param rolePo
	 */
	public void sendAvatarRemoveToAreaPlayersFiltedRoundAvatarMapping(BaseRolePo rolePo) {
		ConcurrentHashMap<Integer, BaseMapAvatarVo> list =getAreaAvatarsCache().get(rolePo.getAreaId());
		//为每个用户增添
		for (BaseMapAvatarVo mapAvatarVo : list.values()) {
			List<BaseMapAvatarVo> lists =getRoleIdFiltedRoundAvatarMapping().get(mapAvatarVo.getId());
			for (BaseMapAvatarVo mapAvatarVo2 : lists) {
				if(mapAvatarVo2.getId().intValue()==rolePo.getId().intValue()){
					lists.remove(mapAvatarVo2);
					break;
				}
			}
		}
	}
}
